<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
	String id = request.getParameter("id");
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<base href="<%=basePath%>" />
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<meta http-equiv="X-UA-Compatible" content="IE=7" />
		<title>report right</title>
		<link id="editCss" rel="stylesheet" type="text/css"
			href="<%=basePath%>css/right.css" />
		<link id="editCss" rel="stylesheet" type="text/css"
			href="<%=basePath%>css/right.css" />
		<link id="editCss" rel="stylesheet" type="text/css"
			href="<%=basePath%>css/style.css" />

		<script type="text/javascript" src="<%=basePath%>js/base.js"></script>
		<!-- dwr -->
		<script type='text/javascript'
			src='<%=basePath%>dwr/interface/permission.js'></script>
		<script type='text/javascript' src='<%=basePath%>dwr/engine.js'></script>
	</head>
	<script type="text/javascript"> 
		var ie7 = (navigator.appName == "Microsoft Internet Explorer" && navigator.appVersion.match(/7./i) == '7.') ;
		
		function form_Submit(){
			if(validValue()){
				//抓list.jsp的document
				var list_doc = null ;
				
				if(ie7){
					list_doc = top.frames["rightIframe"];
				}else{
					list_doc = top.frames[1] ;
				}
				
		 		//textValue = textValue.replace(/^\s*/, "").replace(/\s*$/, "");
			
				 if(<%=id != null%>){
				    //返回值给父窗口 
					document.forms[0].action = "<%=basePath%>servlet/role?action=update";
					document.forms[0].submit();
				 	returnValueToParent(list_doc);
					closeMe('role_add');
				 }else{ 
					var id = document.getElementById("roleId").value ;
		 			var name = document.getElementById("roleName").value;
		 			var des = document.getElementById("roleDes").value;

				 	permission.saveRole(id,name,des,function(result){	
				 		if(result=="success"){
					 		list_doc.document.forms[0].action="<%=basePath%>servlet/rolePage";
					 		list_doc.document.forms[0].submit();
				 		}
				 		closeMe('role_add');
				 	});
				 }
			}
		 } 
		 
		 function returnValueToParent(list_doc){
			var table = list_doc.document.getElementById("tableList") ;
			update_return(table);
		 } 
				  
		 function update_return(table){
		 	var rows = table.rows ;
		 	var row = null;
		 	var id = "" ;
		 	var textValue = "" ;
		 	for(var i=0;i<rows.length;i++){
		 		row = rows[i];
		 		id = row.cells[1].innerHTML;
		 		//innerHTML 可能会有空格 ,去掉 
		 		id = id.trim();
		 		textValue = document.getElementById("roleId").value ;
		 		//textValue = textValue.replace(/^\s*/, "").replace(/\s*$/, "");
		 		if(id==document.getElementById("roleId").value){
		 			row.cells[2].innerHTML = document.getElementById("roleName").value;
		 			row.cells[3].innerHTML = document.getElementById("roleDes").value;
		 		}
		 	}
		 }
		 
		 function validValue(){
			 var idStr = document.getElementById("roleId").value ;
			 var nameStr = document.getElementById("roleName").value ;
			 var result = false ;  
			 if( idStr==null || idStr=="" ||nameStr==null||nameStr==""){
			   	alert("角色ID、角色名称 均不可为空.");
			 	return result;
			 }
			 
			 var reg = /[\u4e00-\u9fa5]/; 
			 if(reg.test(idStr)){
			 	alert("角色id 不能含有中文。");
			 	return result ;
			 }
			 
			 if(idStr.length > 10){
			 	alert("角色id 不能超过10个字符.");
				return  result;
			 }
			 
			 return result=true ;
		 }
	</script>
	<body>
		<form action="" method="post">
			<div style="height: 145px">
				<div class="crumbs">
					&nbsp;当前位置： 角色管理 &gt; 添加角色
				</div>
				<div class="tableBox" style="margin-top: 5px">
					<table cellPadding="0" cellSpacing="0" border="0" width="100%"
						id="tableList"
						style="border-collapse: collapse; empty-cells: show;">
						<tbody>
							<tr class="">
								<td width="120">
									角色ID
								</td>
								<td>
									<%
										if (id != null) {
									%>
									<%=id%>
										<input type="hidden" id="roleId" name="id" size="24"
											value="<%=id%>" />
									<%
										} else {
									%>
										<input type="text" id="roleId" name="id" size="24" />
										<span style="color: red"> 不能超过10个字符</span>
									<%
										}
									%>
								</td>
							</tr>
							<tr>
								<td>
									角色名称
								</td>
								<td>
									<input type="text" id="roleName" class="noborder" name="name"
										size="45%"
										value="<%=request.getParameter("name")%>" />
								</td>
							</tr>
							<tr>
								<td>
									角色描述
								</td>
								<td>
									<input type="text" id="roleDes" class="noborder"
										name="discription" size="45%"
										value="<%=request.getParameter("des")%>" />
								</td>
							</tr>
						</tbody>
					</table>
				</div>
			</div>
			<div class="popUpFooter">
				<input type="button" class="button" value=" 保存 "
					onclick="javascript:form_Submit()" />
				<input type="button" class="button" value=" 取消 "
					onclick="closeMe('role_add')" />
			</div>
		</form>
	</body>

</html>